﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Practices.ServiceLocation;

using Happy.ExtentionMethods;

namespace Happy.Domain
{
    /// <summary>
    /// 扩展<see cref="IUnitOfWork"/>
    /// </summary>
    public static class IUnitOfWorkExtentions
    {
        /// <summary>
        /// 返回工作单元管理的仓储。
        /// </summary>
        public static TRepository CreateRepository<TRepository>(this IUnitOfWork that)
            where TRepository : IRepository
        {
            that.MustNotNull("that");

            if (typeof(TRepository).IsInterface)
            {
                return ServiceLocator.Current.GetInstance<TRepository>();
            }
            else
            {
                return (TRepository)Activator.CreateInstance(typeof(TRepository), that);
            }
        }
    }
}
